package de.carcophan.jimdb.jsf.mb;

import de.carcophan.jimdb.ejb.entites.Status;
import de.carcophan.jimdb.ejb.entites.UserMovies;
import de.carcophan.jimdb.jsf.backing.StatusBackingBean;
import java.util.LinkedList;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Observes;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.inject.Inject;

@Named(value = "movieController")
@SessionScoped
public class MovieController extends Controller {

    @Inject
    private StatusBackingBean backingBean;

    public List<Status> getStatusFilterValues() {
        this.statusFilterValues = new LinkedList<Status>(backingBean.getStatuses());
        statusFilterValues.add(0, new Status("ALL"));
        return statusFilterValues;
    }
    private DataModel<UserMovies> items = null;
    private List<Status> statusFilterValues = null;
    private String selectedFilter;

    /** Creates a new instance of MovieController */
    public MovieController() {
    }

    /**
     * called by f:event in movieTable.xhtml
     */
    @Override
    public void refreshDataList(@Observes UserMovies um) {
        if (selectedFilter == null || selectedFilter.equals("ALL")) {
            this.items = new ListDataModel<UserMovies>(getUserMoviesFacade().findMoviesByUserId(getAuthenticatedUser().getId()));
        } else {
            this.items = new ListDataModel<UserMovies>(getUserMoviesFacade().findByStatus(selectedFilter, getAuthenticatedUser().getId()));
        }
        sortMovies();
        setItems(items);
    }

    public void applyFilter() {
    }

    @Override
    public DataModel<UserMovies> getItems() {
        if (this.items == null) {
            refreshDataList(null);
        }
        return items;
    }

    @Override
    public void saveSelectedMovie() {
    }

    @Override
    public void setItems(DataModel<UserMovies> items) {
        this.items = items;
    }

    public String getSelectedFilter() {
        return selectedFilter;
    }

    public void setSelectedFilter(String selectedFilter) {
        this.selectedFilter = selectedFilter;
        refreshDataList(null);

    }
}
